package com.example;

import com.example.anno.AppConfig;
import com.example.bean.Product;
import com.example.bean.User;
import com.example.bean.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Date 2022/1/15
 * @Created by Jonathan
 */
@Slf4j
public class SpringTest {

	/**
	 * 测试自定义标签问题
	 */
	public static void test01() {
		ApplicationContext app = new ClassPathXmlApplicationContext("suns.xml");
		Object u = app.getBean("u");
	}

	/**
	 * 测试父子容器问题
	 */
	public static void test02() {
		DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(parent);
		xmlBeanDefinitionReader.loadBeanDefinitions("parent.xml");

		DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
		XmlBeanDefinitionReader xmlBeanDefinitionReader1 = new XmlBeanDefinitionReader(child);
		xmlBeanDefinitionReader1.loadBeanDefinitions("child.xml");

		// 一旦使用父子容器  最终父子容器的配置信息融合
		// 如果遇到同名的配置 使用子容器

		User u = (User) child.getBean("u");
		Product p = child.getBean("p", Product.class);


		System.out.println("u=" + u);
		System.out.println("p=" + p);
	}

	/**
	 * 测试父子Bean
	 */

	public static void test03() {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
		xmlBeanDefinitionReader.loadBeanDefinitions("parent&childbean.xml");
		User bean = beanFactory.getBean(User.class);
		System.out.println(bean);
	}

	/**
	 * debug反射创建对象
	 */

	private static void test04() {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
		xmlBeanDefinitionReader.loadBeanDefinitions("reflect.xml");
		User bean = (User) beanFactory.getBean("u");
		System.out.println(bean);

	}

	private static void test05() {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
		Object person = ctx.getBean("person");
		System.out.println(person);
	}

	/**
	 * 测试AOP编程
	 */
	private static void test06() {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
		xmlBeanDefinitionReader.loadBeanDefinitions("aoptest.xml");
		UserService userService = (UserService) beanFactory.getBean("userService");
		userService.register("zhangsan", "password");
	}

	public static void main(String[] args) {
		//test01();
		//test02();
		//test03();
		//test04();
		//test05();
		test06();
	}
}
